package cn.wolfcode.education.web.controller;

import cn.wolfcode.education.anno.PermissionName;
import cn.wolfcode.education.domain.Collections;
import cn.wolfcode.education.query.CollectionsQuery;
import cn.wolfcode.education.service.ICollectionsService;
import cn.wolfcode.education.util.JSONResult;
import cn.wolfcode.education.util.PageResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("collections")
public class CollectionsController {
    @Autowired
    private ICollectionsService collectionsService;

    @RequestMapping("")
    @PermissionName("收款管理")
    @RequiresPermissions("collections:view")
    public String index(){
        return "collections/collections";
    }

    @RequestMapping("query")
    @ResponseBody
    @PermissionName("收款列表")
    @RequiresPermissions("collections:query")
    public PageResult query(CollectionsQuery qo){
        return collectionsService.query(qo);
    }

    @RequestMapping("update")
    @ResponseBody
    @PermissionName("收款更新")
    @RequiresPermissions("collections:update")
    public JSONResult update(Collections collections){
        JSONResult jsonResult = new JSONResult();
        try {
            collectionsService.update(collections);
        }catch (Exception e){
            jsonResult.setMsg("更新失败");
        }
        return jsonResult;
    }

    @RequestMapping("auditCollections")
    @ResponseBody
    @PermissionName("收款审核")
    @RequiresPermissions("collections:auditCollections")
    public JSONResult auditCollections(Long id){
        JSONResult jsonResult = new JSONResult();
        try {
            collectionsService.auditCollections(id);
        }catch (Exception e){
            jsonResult.setMsg(e.getMessage());
        }
        return jsonResult;
    }

    @RequestMapping("unauditCollections")
    @ResponseBody
    @PermissionName("收款反审核")
    @RequiresPermissions("collections:unauditCollections")
    public JSONResult unauditCollections(Long id){
        JSONResult jsonResult = new JSONResult();
        try {
            collectionsService.unauditCollections(id);
        }catch (Exception e){
            jsonResult.setMsg("反审核失败");
        }
        return jsonResult;
    }
}